/**
	Pulsar engine. Core.
	File manager implementation - windows extension

	Copyright (C) 2013-2014 Dmitry Dolgov

	This program is free software; you can redistribute it and/or modify it under the terms of the
	GNU General Public License as published by the Free Software Foundation; either version 2 of
	the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
	without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
	See the GNU General Public License for more details.
*/

#include <pulsar/pulsar.h>
#include <pulsar/src/pulsar_intr.h>

pl::FileManagerImpl* pl::FileManagerImpl::createImpl()
{
	return PL_NEW FileManagerImplWin;
}

/**
*/
pl::FileManagerImplWin::FileManagerImplWin()
{
	return;
}

/**
*/
pl::FileManagerImplWin::~FileManagerImplWin()
{
	return;
}

bool pl::FileManagerImplWin::createDrives()
{
	PL_NEW DriveWin;
	return true;
}

/**
*/
bool pl::FileManagerImplWin::convertWin2Fs(DWORD attribWin, pl_uint32& attribFs)
{
	attribFs = 0;

	if (attribWin & FILE_ATTRIBUTE_ARCHIVE)
	{
		attribFs |= FILE_ATTR_ARCHIVE;
	}
	if (attribWin & FILE_ATTRIBUTE_COMPRESSED)
	{
		attribFs |= FILE_ATTR_COMPRESSED;
	}
	if (attribWin & FILE_ATTRIBUTE_DIRECTORY)
	{
		attribFs |= FILE_ATTR_DIRECTORY;
	}
	if (attribWin & FILE_ATTRIBUTE_ENCRYPTED)
	{
		attribFs |= FILE_ATTR_ENCRYPTED;
	}
	if (attribWin & FILE_ATTRIBUTE_HIDDEN)
	{
		attribFs |= FILE_ATTR_HIDDEN;
	}
	if (attribWin & FILE_ATTRIBUTE_READONLY)
	{
		attribFs |= FILE_ATTR_READONLY;
	}
	if (attribWin & FILE_ATTRIBUTE_SYSTEM)
	{
		attribFs |= FILE_ATTR_SYSTEM;
	}
	if (attribWin & FILE_ATTRIBUTE_TEMPORARY)
	{
		attribFs |= FILE_ATTR_TEMPORARY;
	}
	return true;
}

/**
*/
bool pl::FileManagerImplWin::convertFs2Win(pl_uint32 attribFs, DWORD& attribWin)
{
	attribWin = 0;

	if (attribFs & FILE_ATTR_ARCHIVE)
	{
		attribWin |= FILE_ATTRIBUTE_ARCHIVE;
	}
	if (attribFs & FILE_ATTR_COMPRESSED)
	{
		attribWin |= FILE_ATTRIBUTE_COMPRESSED;
	}
	if (attribFs & FILE_ATTR_DIRECTORY)
	{
		attribWin |= FILE_ATTRIBUTE_DIRECTORY;
	}
	if (attribFs & FILE_ATTR_ENCRYPTED)
	{
		attribWin |= FILE_ATTRIBUTE_ENCRYPTED;
	}
	if (attribFs & FILE_ATTR_HIDDEN)
	{
		attribWin |= FILE_ATTRIBUTE_HIDDEN;
	}
	if (attribFs & FILE_ATTR_READONLY)
	{
		attribWin |= FILE_ATTRIBUTE_READONLY;
	}
	if (attribFs & FILE_ATTR_SYSTEM)
	{
		attribWin |= FILE_ATTRIBUTE_SYSTEM;
	}
	if (attribFs & FILE_ATTR_TEMPORARY)
	{
		attribWin |= FILE_ATTRIBUTE_TEMPORARY;
	}
	return true;
}

/**
*/
pl::FileError pl::FileManagerImplWin::copyFile(const pl::Filepath& oldName, const pl::Filepath& newName, bool bForce)
{
	DWORD attrib = GetFileAttributes(newName.get());

	if (attrib != (DWORD)-1)
	{
		if (attrib & FILE_ATTRIBUTE_DIRECTORY)
		{
			return pl::FILE_ERROR_DIRECTORY_EXIST;
		}
		if (attrib & FILE_ATTRIBUTE_READONLY)
		{
			SetFileAttributes(newName.get(), FILE_ATTRIBUTE_NORMAL);
		}
	}
	if (bForce)
	{
		pl::FileManager::makeDir(pl::removeFilename(newName), true);
	}

	if (!CopyFile(oldName.get(), newName.get(), FALSE))
	{
		plLogError(PL_LOG_NAME, PL_T("CopyFile() failed with error ") << GetLastError());
		return pl::FILE_ERROR_FILE_NOT_FOUND;
	}
	return pl::FILE_ERROR_NONE;
}

/**
*/
bool pl::FileManagerImplWin::convertToFullPath(pl::Filepath& fileName)
{
	pl_char buffer[MAX_PATH];
	if (!GetCurrentDirectory(MAX_PATH, buffer))
	{
		return false;
	}
	fileName = mergeFilename(buffer, fileName);
	return true;
}


/**
*/
void pl::FileManagerImplWin::_convertParams(pl_uint32 options, DWORD& share, DWORD& openAttrib)
{
	share = 0;

	if (options & OPEN_FILE_SHARE_READ)
	{
		share |= FILE_SHARE_READ;
	}
	if (options & OPEN_FILE_SHARE_WRITE)
	{
		share |= FILE_SHARE_WRITE;
	}
	openAttrib = 0;

	if (options & OPEN_FILE_CREATENEW)
	{
		openAttrib |= CREATE_NEW;
	}
	if (options & OPEN_FILE_OPENEXIST)
	{
		openAttrib |= OPEN_EXISTING;
	}
	if (options & OPEN_FILE_CREATEALWAYS)
	{
		openAttrib |= CREATE_ALWAYS;
	}
	if (options & OPEN_FILE_OPENALWAYS)
	{
		openAttrib |= OPEN_ALWAYS;
	}
	if (options & OPEN_FILE_APPEND)
	{
		openAttrib |= OPEN_ALWAYS;
	}
	return;
}

//
// End of file 'pulsar_file_manager_impl_win.cpp'
//
